﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint;

namespace MS.InformationManagement.DAL.RecordCenter
{
    public partial class Item : ICustomMapping
    {

        private DateTime _created;
        private string _author;
        private int _authorId;

        public DateTime Created
        {
            get { return _created; }

        }

        public string Author
        {
            get { return _author; }
        }

        public int AuthorId
        {
            get { return _authorId; }
        }

        [CustomMapping(Columns = new String[] { "Created", "Author" })]
        public void MapFrom(object listItem)
        {
            SPListItem item = (SPListItem)listItem;

            this._created = (DateTime)item[SPBuiltInFieldId.Created];
            if (item[SPBuiltInFieldId.Author] != null)
            {
                SPFieldUserValue userValue = new SPFieldUserValue(item.Web, item[SPBuiltInFieldId.Author].ToString());
                this._authorId = userValue.LookupId;
                this._author = userValue.LookupValue;
            }

        }

        public void MapTo(object listItem) { }


        public void Resolve(RefreshMode mode, object originalListItem, object databaseListItem)
        {
            //throw new NotImplementedException();
        }
    }

    public static class ItemExtender
    { 
        public static string ToCSVString(this EntitySet<TipoDocumental> items){
            StringBuilder toStringValue = new StringBuilder();
            foreach (var item in items) {
                toStringValue
                    .Append(item.Title)
                    .Append(".");
            }
            if (toStringValue.Length > 0) { toStringValue.Remove(toStringValue.Length - 1, 1); }
            return toStringValue.ToString();
        }
    }
}
